package gl.android.biyiqing.fragment;

import gl.adapter.JsonArrayImageListAdapter;
import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.android.biyiqing.competent.OnToUserInfoItemClickListener;
import gl.android.biyiqing.event.EventCalnceFollow;
import gl.android.biyiqing.event.EventOnFollow;
import gl.android.biyiqing.event.GOTOFollow;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.Tip;
import gl.view.TabIndictor;

import org.json.JSONObject;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.huewu.pla.lib.internal.PLA_AdapterView;

import de.greenrobot.event.EventBus;

public class TalkListFragment extends BaseListFragmet {

	private TextView empty;
	private View gofollow;
	private PLA_AdapterView<BaseAdapter> list;
	private PLA_AdapterView<BaseAdapter> list1;
	private ViewSwitcher viewSwitcher;
	String NOTFOUND = "目前没有什么内容";
	private Drawable drawable;

	@SuppressWarnings("unchecked")
	private void bindViews() {

		empty = (TextView) findViewById(R.id.empty);
		list = (PLA_AdapterView<BaseAdapter>) findViewById(android.R.id.list);
		list1 = (PLA_AdapterView<BaseAdapter>) findViewById(R.id.list1);
		gofollow =  findViewById(R.id.gofollow);
		viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});

		empty.setText(NOTFOUND);

		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
		viewSwitcher.setDisplayedChild(0);
		((RadioGroup) rootView.findViewById(R.id.RadioGroup))
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						viewSwitcher.setDisplayedChild(TabIndictor
								.getIndexInRadioGroup((RadioGroup) rootView
										.findViewById(R.id.RadioGroup),
										checkedId));

					}
				});
	}

	public static TalkListFragment newInstance() {
		TalkListFragment fragment = new TalkListFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}
	public void onEvent(EventOnFollow event){
		if (isValueFragment()) {
			initdata();
		}
		
	}
	public void onEvent(EventCalnceFollow event){
		if (isValueFragment()) {
			initdata();
		}
		
	}
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		EventBus.getDefault().register(this);
	}
	@Override
	public void onDetach() {
		super.onDetach();
		EventBus.getDefault().unregister(this);
	}
	public TalkListFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_talkl_list, container,
				false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_discovery_pic_btn3);
		bindViews();
		initdata();
		return rootView;
	}

	final String TYPE_LOOK_ME = "1";
	final String TYPE_MY_LOOKED = "2";

	private void initdata() {
		allEmptyCount = 0;
		initListView(list, API.URL + "GetHomeViewerForTalk?userid=%s&type=%s",
				new Object[] { UserManger.getInstance().userid, TYPE_LOOK_ME },
				new int[] { R.layout.item_list_image_card,
						R.layout.item_list_image_card_longer }, new String[] {
						"username", "avatar", BYQUserInfo.KEY_SHENFEN,"nearby",BYQUserInfo.KEY_GROUP,BYQUserInfo.KEY_VIP_UNIT  }, new int[] {
						R.id.tv_nick, R.id.iv_avator, R.id.age ,R.id.tv_place,R.id.iv_doman,R.id.iv_vip,R.id.iv_sendmsg,R.id.iv_call});
		initListView(
				list1,
				API.URL + "GetHomeViewerForTalk?userid=%s&type=%s",
				new Object[] { UserManger.getInstance().userid, TYPE_MY_LOOKED },
				new int[] { R.layout.item_list_image_card,
						R.layout.item_list_image_card_longer }, new String[] {
						"username", "avatar", BYQUserInfo.KEY_SHENFEN ,"nearby",BYQUserInfo.KEY_GROUP,BYQUserInfo.KEY_VIP_UNIT}, new int[] {
						R.id.tv_nick, R.id.iv_avator, R.id.age ,R.id.tv_place,R.id.iv_doman,R.id.iv_vip,R.id.iv_sendmsg,R.id.iv_call });

	}
	@Override
	protected String preFormatJsonData(String t) {
		return BYQUserInfo.preFormatShenFen(t);
	}
	private int allEmptyCount;
	private void initListView(final PLA_AdapterView<BaseAdapter> adpateview,
			String url, Object[] pars, final int[] itemLayouts,
			final String[] from, final int[] to) {
		BaseControl.get(url, new AjaxCallBackProxy() {
			

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				if (!isValueFragment()) {
					return;
				}
				Tip.show(NOTFOUND);
			}

			@Override
			public void onSuccess(String t) {
				if (!isValueFragment()) {
					return;
				}
				super.onSuccess(t);
				try {
					t = preFormatJsonData(t);
					if (new JSONObject(t).getJSONArray("data") == null) {
						Tip.show(NOTFOUND);
						return;
					}
					dataJSONArray = new JSONObject(t).getJSONArray("data");
					adpateview.setAdapter(new JsonArrayImageListAdapter(
							getActivity(), new JSONObject(t).get("data")
									.toString(), itemLayouts, from, to,getOnItemChildViewCilck()));
					adpateview
							.setOnItemClickListener(new OnToUserInfoItemClickListener(
									getActivity(), new JSONObject(t)
											.get("data").toString(), from));
					if (/*adpateview == list1&&*/dataJSONArray.length()<=0) {//my look
						allEmptyCount++;
						if (allEmptyCount>=2) {
							gofollow.setVisibility(View.VISIBLE);
							gofollow.setOnClickListener(new View.OnClickListener() {
								
								@Override
								public void onClick(View v) {
									EventBus.getDefault().post(new GOTOFollow());
									
								}
							});
						}
						
					}else{
						gofollow.setVisibility(View.GONE);
					}
				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}

			}
		}, pars);
	}
}